/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2021-10-03 10:06:15
 * @LastEditors: TT
 * @LastEditTime: 2023-03-08 22:19:24
 */

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../comment_config/comment_colors.dart';
import '../page_state.dart';

class CommentTools {

  // 根据列表数据 配置界面状态
  static configPageState({
    required int allNum,
    required int networkNum,
  }) {
    PageState st = PageState.dataFetchState;
    if (allNum == 0) {
      st = PageState.emptyDataState;
    } else if (networkNum == 0) {
      st = PageState.noMoreDataState;
    }
    return st;
  }

  // 配置 yyyy-mm-dd
  static String configyyyymmdd(int year, int m, int d) {
    String ystr = "$year";
    String mstr = "$m";
    String dStr = '$d';
    if (m < 10) {
      mstr = "0$m";
    }
    if (d < 10) {
      dStr = "0$d";
    }
    return ystr + "-" + mstr + "-" + dStr;
  }

  // 配置几分钟前
  static String messageTime(int timeStamp) {
    int time = DateTime.now().millisecondsSinceEpoch;
    String msg = '';
    int _distance = ((time - timeStamp) / 1000).round();
    if (_distance <= 60) {
      msg = '刚刚';
    } else if (_distance <= 3600) {
      msg = '${(_distance / 60).floor()}分钟前';
    } else if (_distance <= 43200) {
      msg = '${(_distance / 60 / 60).floor()}小时前';
    } else if (DateTime.fromMillisecondsSinceEpoch(time).year ==
        DateTime.fromMillisecondsSinceEpoch(timeStamp).year) {
      msg = commentformatDate(DateTime.fromMillisecondsSinceEpoch(timeStamp),
          format: 'MM-dd HH:mm');
    } else {
      msg = commentformatDate(DateTime.fromMillisecondsSinceEpoch(timeStamp),
          format: 'YY-MM-dd HH:mm');
    }

    return msg;
  }

  static String commentformatDate(DateTime? dateTime, {String? format}) {
    if (dateTime == null) return '';
    format = format ?? "yyyy-MM-dd HH:mm:ss";
    if (format.contains('yy')) {
      String year = dateTime.year.toString();
      if (format.contains('yyyy')) {
        format = format.replaceAll('yyyy', year);
      } else {
        format = format.replaceAll(
            'yy', year.substring(year.length - 2, year.length));
      }
    }

    format = commentcomFormat(dateTime.month, format, 'M', 'MM');
    format = commentcomFormat(dateTime.day, format, 'd', 'dd');
    format = commentcomFormat(dateTime.hour, format, 'H', 'HH');
    format = commentcomFormat(dateTime.minute, format, 'm', 'mm');
    format = commentcomFormat(dateTime.second, format, 's', 'ss');
    format = commentcomFormat(dateTime.millisecond, format, 'S', 'SSS');

    return format;
  }

  /// com format.
  static String commentcomFormat(
      int value, String format, String single, String full) {
    if (format.contains(single)) {
      if (format.contains(full)) {
        format =
            format.replaceAll(full, value < 10 ? '0$value' : value.toString());
      } else {
        format = format.replaceAll(single, value.toString());
      }
    }
    return format;
  }

  /// 获取图片地址
  static String getImagePath(String name, {String format = 'png'}) {
    return 'assets/images/$name.$format';
  }

  // 校验手机号
  static bool chinaPhoneNumber(String input) {
    if (input.isEmpty) return false;
    //手机正则验证
    String regexPhoneNumber =
        "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$";
    return RegExp(regexPhoneNumber).hasMatch(input);
  }

  static Image configImageasset(String path, double? width, double? height,
      {String accets = '', BoxFit fit = BoxFit.none}) {
    return Image.asset(
      path,
      width: width,
      height: height,
      fit: fit,
    );
  }

  String formatNum(double num, {int postion = 2}) {
    String money = formartNum(num, 2);
    return money;
  }

  String formartNum(num target, int postion, {bool isCrop = false}) {
    String t = target.toString();
    // 如果要保留的长度小于等于0 直接返回当前字符串
    if (postion < 0) {
      return t;
    }
    if (t.contains(".")) {
      String t1 = t.split(".").last;
      if (t1.length >= postion) {
        if (isCrop) {
          // 直接裁剪
          return t.substring(0, t.length - (t1.length - postion));
        } else {
          // 四舍五入
          return target.toStringAsFixed(postion);
        }
      } else {
        // 不够位数的补相应个数的0
        String t2 = "";
        for (int i = 0; i < postion - t1.length; i++) {
          t2 += "0";
        }
        return t + t2;
      }
    } else {
      // 不含小数的部分补点和相应的0
      String t3 = postion > 0 ? "." : "";

      for (int i = 0; i < postion; i++) {
        t3 += "0";
      }
      return t + t3;
    }
  }

  // 通用间隔
  static Widget configlineSpace(
      {double height = 0.5, Color color = CommentColorS.cole5e5e5}) {
    if (color == CommentColorS.cole5e5e5) {
      color = CommentColorS.cole5e5e5;
    }
    return Container(height: height, color: color);
  }

  // 设置键盘 隐藏
  static keydissmiss(BuildContext context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }
  }

  // 赋值剪切板内容
  static setDataToClipboard({String? text, Function? copyBlock}) async {
    await Clipboard.setData(ClipboardData(text: text));
    if (copyBlock != null) {
      copyBlock();
    }
  }

  // 获取剪切板内容
  static getDataToClipboard() {
    return Clipboard.getData(Clipboard.kTextPlain);
  }
}

class SizeMacro {
  // 屏幕宽
  static double screenWidth = ScreenUtil().screenWidth;
  // 屏幕高
  static double screenHeight = ScreenUtil().screenHeight;
  // 系统状态栏高度
  static double statusBarHeight = ScreenUtil().statusBarHeight;
  // 底部安全举例距离BottomBar高度
  static double bottomBarHeight = ScreenUtil().bottomBarHeight;
  // 导航栏的高度
  static double navh = statusBarHeight + 44;

  // 获取适配  width 宽
  static double setWidth(double width) {
    return width.w;
  }

  // 获取适配  height 高
  static double setHeight(double height) {
    return height.h;
  }
}

class Dimens {
  static double font_12 = 12.sp;
  static double font_14 = 14.sp;
  static double font_16 = 16.sp;
  static double font_18 = 18.sp;
  static double font_20 = 20.sp;
  static double font_22 = 22.sp;
  static double font_24 = 24.sp;
  static double font_26 = 26.sp;
  static double font_28 = 28.sp;
  static double font_30 = 30.sp;

  static double dp5 = 5.w;
  static double dp10 = 10.w;
  static double dp15 = 15.w;
  static double dp20 = 20.w;
  static double dp25 = 25.w;
  static double dp30 = 30.w;
  static double dp35 = 35.w;
  static double dp40 = 40.w;
  static double dp45 = 45.w;
  static double dp50 = 50.w;
}

class Gaps {
  /// 水平间距
  static Widget hGap5 = confighbox(Dimens.dp5);
  static Widget hGap10 = confighbox(Dimens.dp10);
  static Widget hGap15 = confighbox(Dimens.dp15);
  static Widget hGap20 = confighbox(Dimens.dp20);
  static Widget hGap25 = confighbox(Dimens.dp25);
  static Widget hGap30 = confighbox(Dimens.dp30);
  static Widget hGap35 = confighbox(Dimens.dp35);
  static Widget hGap40 = confighbox(Dimens.dp40);
  static Widget hGap45 = confighbox(Dimens.dp45);
  static Widget hGap50 = confighbox(Dimens.dp50);

  /// 垂直间距
  static Widget vGap5 = configvbox(Dimens.dp5);
  static Widget vGap10 = configvbox(Dimens.dp10);
  static Widget vGap15 = configvbox(Dimens.dp15);
  static Widget vGap20 = configvbox(Dimens.dp20);
  static Widget vGap25 = configvbox(Dimens.dp25);
  static Widget vGap30 = configvbox(Dimens.dp30);
  static Widget vGap35 = configvbox(Dimens.dp35);
  static Widget vGap40 = configvbox(Dimens.dp40);
  static Widget vGap45 = configvbox(Dimens.dp45);
  static Widget vGap50 = configvbox(Dimens.dp50);

  static confighbox(double width) {
    return SizedBox(width: width);
  }

  static configvbox(double height) {
    return SizedBox(height: height);
  }
}
